<?php
class Thread_model extends CI_Model {
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    public function find_by_id ($id) {
    	$this->db->where('id', intval($id));
		return $this->db->get('threads')->row_array();
    }
    /**
     * 群组发帖数量
     * @param int $group_id
     */
	public function get_count_by_group_id ($group_id) {
		$sql = " select count(*) as total from threads  where group_id=" . intval($group_id);
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	/**
	 * 群组发帖列表
	 * @param int $group_id
	 * @param int $offset
	 * @param int $limit
	 */
	public function find_by_group_id ($group_id, $offset = 0, $limit = 10) {
		$sql = " select t.*,u.nickname from threads t left join users u on t.created_by=u.id where t.group_id=" . intval($group_id);
		$sql .= " order by t.is_down asc, t.is_top desc ,t.created desc ";
		$sql .= " limit " . $offset . ", " . $limit;
		return $this->db->query($sql)->result_array();
	}
	/**
	 * 新加主贴
	 * @param array $data
	 */
	public function add($data) {
		$content= $data['content'];
		unset($data['content']);
		$this->db->insert("threads", $data);
		$thread_id = $this->db->insert_id();
		$post_data = array('thread_id' => $thread_id,
							'title' => $data['title'],
							'content' => $content,
							'created_by' => $data['created_by'],
							'is_thread' => 1,
							'group_id' => $data['group_id'],
							'created' => $data['created']);
		$this->db->insert("posts", $post_data);
		$post_id = $this->db->insert_id();
		return $thread_id;
	}
	/**
	 * 更新回复次数
	 * @param int $thread_id
	 * @param int $step
	 */
	public function update_reply_count ($thread_id, $step = 1) {
		$sql = " update threads set reply_count = reply_count + (" .$step . ") where id=" . intval($thread_id) . " limit 1";
		return $this->db->query($sql);
	}
	/**
	 * 更新阅读次数
	 * @param int $thread_id
	 * @param int $step
	 */
	public function update_view_count ($thread_id, $step = 1) {
		$sql = " update threads set view_count = view_count + (" .$step . ") where id=" . intval($thread_id) . " limit 1";
		return $this->db->query($sql);
	}
	/**
	 * 删除
	 * @param int $id
	 */
	public function delete($id) {
		$this->db->delete('threads', array('id' => $id)); 
		$this->db->delete('posts', array('thread_id' => $id)); 
	}
	
    /**
     * 群组发帖数量
     * @param int $user_id
     */
	public function get_count_by_user_id ($user_id) {
		$sql = " select count(*) as total from threads  where created_by=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	/**
	 * 群组发帖列表
	 * @param int $user_id
	 * @param int $offset
	 * @param int $limit
	 */
	public function find_by_user_id ($user_id, $offset = 0, $limit = 10) {
		$sql = " select t.*,u.nickname from threads t left join users u on t.created_by=u.id where t.created_by=" . intval($user_id);
		$sql .= " limit " . $offset . ", " . $limit;
		return $this->db->query($sql)->result_array();
	}
	
	/**
	 * 设置某个字段的值
	 * 
	 * @param string $key
	 * @param string $value
	 * @param string $condition
	 */
	public function set_field($key, $value, $condition) {
		$sql = "update threads set $key='" . $value . "' where " . $condition;
		return $this->db->query($sql);
	}
}